home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / wb / SnakeGame.lzh / snake / snake.c < prev    next >
C/C++ Source or Header  |  1992-01-18  |  4KB  |  264 lines

  1. /*
  2.  * snake.c - a simple Amiga game
  3.  *
  4.  * Bruno Costa - 30 apr 89
  5.  *
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <intuition/intuition.h>
  10. #include <functions.h>
  11.  
  12. int retcode = 0;
  13. struct Library *IntuitionBase = NULL;
  14. struct Library *GfxBase = NULL;
  15. struct Window *wd = NULL;
  16. struct RastPort *rp = NULL;
  17.  
  18. #define XSIZE 140
  19. #define YSIZE 88
  20. #define XPIX 3
  21. #define YPIX 2
  22. #define NPOINTS 50
  23. #define MAXLEN 120
  24. #define GROWTHSPEED 7
  25. #define DOTCOL 2
  26. #define SNKCOL 1
  27. #define AIMCOL 3
  28. #define BKGCOL 0
  29.  
  30. struct NewWindow wdattr = {
  31.  0, 11, XPIX*XSIZE + 8, YPIX*YSIZE + 13,
  32.  2, 1, CLOSEWINDOW,
  33.  WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO |
  34.  SMART_REFRESH | ACTIVATE | NOCAREREFRESH,
  35.  NULL, NULL, NULL, NULL, NULL,
  36.  0, 0, 0, 0,
  37.  WBENCHSCREEN
  38. };
  39.  
  40. struct {int x, y;} sn[MAXLEN];
  41. int snhead, sntail, sndir, snlen;
  42. int sndx[4] = {-1, 0, 1, 0 };
  43. int sndy[4] = { 0,-1, 0, 1 };
  44. int score;
  45.  
  46. #define random(max) (rand()%max)
  47.  
  48. Abort(code)
  49. int code;
  50. {
  51.  if (wd)
  52.    CloseWindow(wd);
  53.  if (IntuitionBase)
  54.    CloseLibrary(IntuitionBase);
  55.  if (GfxBase)
  56.    CloseLibrary(GfxBase);
  57.  exit (code);
  58. }
  59.  
  60. setdisplay()
  61. {
  62.  GfxBase = OpenLibrary("graphics.library",0L);
  63.  IntuitionBase = OpenLibrary("intuition.library",0L);
  64.  wd = OpenWindow(&wdattr);
  65.  if (!wd)
  66.    Abort(30);
  67.  rp = wd->RPort;
  68. }
  69.  
  70. plot (x,y,c)
  71. int x,y,c;
  72. {
  73.  register int i, yc;
  74.  int xi, xf;
  75.  
  76.  xi = x * XPIX;
  77.  xf = xi + XPIX - 1;
  78.  yc = y * YPIX;
  79.  SetAPen (rp, (long)c);
  80.  for (i = 0; i < YPIX; yc++, i++)
  81.  {
  82.    Move (rp, (long)xi, (long)yc);
  83.    Draw (rp, (long)xf, (long)yc);
  84.  }
  85. }
  86.  
  87. int rdpix(x,y)
  88. int x,y;
  89. {
  90.  return (ReadPixel(rp, (long)(x * XPIX), (long)(y * YPIX)));
  91. }
  92.  
  93. int plotaim()
  94. {
  95.  int x, y;
  96.  do
  97.  {
  98.    x = random(XSIZE);
  99.    y = random(YSIZE);
  100.  } while (rdpix(x, y) != BKGCOL);
  101.  plot(x, y, AIMCOL);
  102. }
  103.  
  104. dispscore()
  105. {
  106.  char str[20];
  107.  sprintf(str, " Score: %d ", score);
  108.  SetWindowTitles(wd, str, "Snake  v 1.0  - \xa9 1989 Bruno Costa");
  109. }
  110.  
  111. initscreen()
  112. {
  113.  int i;
  114.  
  115.  SetRast(rp,(long)BKGCOL);
  116.  
  117.  for (i = 0; i < XSIZE; i++)
  118.  {
  119.    plot (i, 0, DOTCOL);
  120.    plot (i, YSIZE-1, DOTCOL);
  121.  }
  122.  for (i = 0; i < YSIZE; i++)
  123.  {
  124.    plot (0, i, DOTCOL);
  125.    plot (XSIZE-1, i, DOTCOL);
  126.  }
  127.  
  128.  for (i = 0; i < NPOINTS; i++)
  129.    plot (random(XSIZE),random(YSIZE),DOTCOL);
  130. }
  131.  
  132. initsnake()
  133. {
  134.  snhead = sntail = sndir = 0;
  135.  snlen = 1;
  136.  sn[0].x = XSIZE/2;
  137.  sn[0].y = YSIZE/2;
  138.  plot (sn[0].x, sn[0].y, SNKCOL);
  139. }
  140.  
  141. /* joystick reading routines */
  142. #define JPORT 2
  143. #define JOYADDR (short *)(0xdff008 + 2 * JPORT)
  144. #define CIAADDR (char *)(0xbfe001)
  145.  
  146. int fire()
  147. {
  148.  return (!(*CIAADDR & (64 * JPORT)));
  149. }
  150.  
  151. int joydx()
  152. {
  153.  int t = *JOYADDR;
  154.  
  155.  if (t & 2)
  156.    return 1;
  157.  if (t & 512)
  158.    return -1;
  159.  return 0;
  160. }
  161.  
  162. int joydy()
  163. {
  164.  int t = *JOYADDR >> 1 ^ *JOYADDR;
  165.  
  166.  if (t & 1)
  167.    return 1;
  168.  if (t & 256)
  169.    return -1;
  170.  return 0;
  171. }
  172.  
  173. int testbreak()
  174. {
  175.  struct Message *msg;
  176.  
  177.  if (msg = GetMsg(wd->UserPort))
  178.  {
  179.    ReplyMsg(msg);
  180.    return (1);
  181.  }
  182.  else
  183.    return (0);
  184. }
  185.  
  186. waitbutton()
  187. {
  188.  while(!fire())
  189.  {
  190.    Delay(10L);
  191.    if (testbreak())
  192.      Abort(1);
  193.  }
  194. }
  195.  
  196. play()
  197. {
  198.  int cx, cy;
  199.  int time = 0;
  200.  
  201.  score = 0;
  202.  dispscore();
  203.  initsnake();
  204.  plotaim();
  205.  
  206.  cx = sn[snhead].x;
  207.  cy = sn[snhead].y;
  208.  
  209.  for (;;)
  210.  {
  211.    if (joydx() || joydy())    /* true if joystick moved */
  212.    {
  213.      int dx = joydx();
  214.      int dy = joydy();
  215.      if (!(dx && dy))        /* diagonals are discarded */
  216.        if (dx != -sndx[sndir] || dy != -sndy[sndir])
  217.          if (dx == 1)        sndir = 2;
  218.          else if (dx == -1)    sndir = 0;
  219.          else if (dy == 1)    sndir = 3;
  220.          else            sndir = 1;
  221.    }
  222.    cx += sndx[sndir];
  223.    cy += sndy[sndir];
  224.    if (rdpix(cx,cy))        /* true if collision happened */
  225.    {
  226.      if (rdpix(cx,cy) == AIMCOL)
  227.      {
  228.        score++;
  229.        dispscore();
  230.        plotaim();
  231.      }
  232.      else
  233.        return;
  234.    }
  235.    plot (cx, cy, SNKCOL);
  236.    snhead = (snhead + 1) % MAXLEN;
  237.    if (snlen < MAXLEN && !(time % GROWTHSPEED))
  238.      snlen++;
  239.    else
  240.    {
  241.      plot (sn[sntail].x, sn[sntail].y, BKGCOL);
  242.      sntail = (sntail + 1) % MAXLEN;
  243.    }
  244.    sn[snhead].x = cx;
  245.    sn[snhead].y = cy;
  246.    if (testbreak())
  247.      Abort(1);
  248.    Delay(1L);
  249.    time++;
  250.  }
  251. }
  252.  
  253. main()
  254. {
  255.  setdisplay();
  256.  for (;;)
  257.  {
  258.    initscreen();
  259.    play();
  260.    dispscore();
  261.    waitbutton();
  262.  }
  263. }
  264.